package com.hyj.shop.project.user.controller;

import com.hyj.shop.project.utils.domain.AjaxResult;
import com.hyj.shop.project.user.domain.UserInfo;
import com.hyj.shop.project.user.service.UserInfoService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/api")
public class UserInfoController {
    @Value("${spring.tengxun.accessKey}")
    private String accessKey;
    @Value("${spring.tengxun.secretKey}")
    private String secretKey;
    @Value("${spring.tengxun.bucket}")
    private String bucket;
    @Value("${spring.tengxun.bucketName}")
    private String bucketName;
    @Value("${spring.tengxun.path}")
    private String path;
    @Value("${spring.tengxun.qianzui}")
    private String qianzui;
    @Autowired
    private UserInfoService userInfoService;
    @RequestMapping(value = "/update/nickName")
    @ResponseBody
    public AjaxResult updateNickName(@RequestBody UserInfo userInfo){
        System.out.println(58);
        System.out.println(userInfo);
       return userInfoService.updateNickName(userInfo);
    }
    @PutMapping(value = "/update/userInfo")
    @ResponseBody
    public AjaxResult updateUserInfo( @RequestParam(required = false) MultipartFile file, String content, String type ){

        System.out.println(type);
        System.out.println(content);

        return userInfoService.updateUserInfo(file,content,type);
    }
    @RequestMapping(value = "/query/userInfo")
    @ResponseBody
    public AjaxResult queryUserInfo(@RequestParam int userId){

        return userInfoService.queryUserInfo(userId);
    }
    @RequestMapping(value = "/upload/avatar/{userId}")
    @ResponseBody
    public AjaxResult uploadUserAvatar(@PathVariable("userId") int userId, @RequestParam("file") MultipartFile file) throws FileNotFoundException {
        AjaxResult ajaxResult=AjaxResult.success();
        if(file == null){
            ajaxResult.put("msg","文件为空");
            return ajaxResult;
        }
        String oldFileName = file.getOriginalFilename();
        String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID()+eName;
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month=cal.get(Calendar.MONTH);
        int day=cal.get(Calendar.DATE);
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
        System.out.println(accessKey+" "+secretKey);
        // 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(bucket));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
        String bucketName = this.bucketName;

        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
        // 大文件上传请参照 API 文档高级 API 上传
        File localFile = null;
        try {
            localFile = File.createTempFile("temp",null);
            file.transferTo(localFile);
            // 指定要上传到 COS 上的路径
//            String key = "/"+this.qianzui+"/"+year+"/"+month+"/"+day+"/"+newFileName;
            String key="/user/avatar/"+newFileName;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
            userInfoService.updateAvatar(userId,this.path + putObjectRequest.getKey());
            System.out.println(putObjectResult+"********");
            ajaxResult.put("msg","上传成功");
            ajaxResult.put("data",this.path + putObjectRequest.getKey());
            return ajaxResult;
        } catch (IOException e) {
            ajaxResult.put("error",e);
            return  ajaxResult;
        }finally {
            // 关闭客户端(关闭后台线程)
            cosclient.shutdown();
        }

    }





}
